var express = require('express');
var router = express.Router();
var usersModel = require('../models/users');
router.post('/user/login', function (req, res) {
    var user = req.body;
    usersModel.login(user, function (user, msg) {
        if (user.length > 0) {
            req.session.user = user;
            res.status(200).json({status: true, msg: '登录成功', data: user});
        } else {
            res.status(200).json({status: false, msg: msg});
        }
    });
});
router.post('/user/register', function (req, res) {
    var user = req.body || {};
    usersModel.register(user, function (msg) {
        res.status(200).json({status: true, msg: msg});
    })
});
router.get('/user/isLogin', function (req, res) {
    if (req.session.user == undefined) {
        res.status(200).json({status: false, msg: '请去登录'});
    } else {
        res.status(200).json({status: true, msg: '登录验证成功', data: req.session.user});
    }
});
router.get('/user/loginoff', function (req, res) {
    delete req.session.user;
    res.status(200).json({status: true, msg: '退出成功!'});
});
router.post('/user/updateinfo', function (req, res) {
    var username = req.body.username,
        user = req.body;
    usersModel.update(username, user, function (msg) {
        res.status(200).json({status: true, msg: msg});
    });
});
router.post('/user/getuser',function (req,res) {
    var user=req.body;
    usersModel.getUser(user,function (user,msg) {
        if(user.length>0){
            res.status(200).json({status:true,msg:'用户信息获取成功',data:user});
        }else {
            res.status(200).json({status:false,msg:msg});
        }
    })
});
module.exports = router;